<?php
/**********************************************************************************
 
 	Display more detailed information for a recipe
	
	Copyright (C) 2010 Benjamin Thomas
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 	Author: Benjamin Thomas
	Email: thomas.ben@gmail.com
 	Created: 7/31/10
 	Revised: 
 
 ***********************************************************************************
 */

require_once(dirname(__FILE__) . '/../../lib/MarketDB.php');
require_once(dirname(__FILE__) . '/../../lib/dateFunctions.php');
require_once(dirname(__FILE__) . '/../../lib/PageLayout.php');
require_once(dirname(__FILE__) . '/../../lib/Recipe.php');

if (array_key_exists('r', $_GET)) {
	$id = intval($_GET['r']);
} else {
	include("prof.php");
	exit();
}
$db = new MarketDB();
$t = new Recipe($id, $db);
$t->calculateCosts();
$recipe = $db->getRecipe($id);
$db->loadItems($recipe['productID']);
$profName = array(1=>'Alchemy',2=>'Blacksmithing',4=>'Enchanting',5=>'Engineering',6=>'General',7=>'Inscription',8=>'Jewelcrafting',9=>'Leatherworking',10=>'Mining',11=>'Tailoring');

$top = array();
if ($recipe['productID']) {
	if (!$icon = $db->items[$recipe['productID']]['icon']) {
			$icon = "404.png";
	}
	echo '<img src="images/icons/'. $icon . '" style="height:20px; width: 20px; margin-right: 5px;" />' . $db->items[$recipe['productID']]['name'] . '<br />';
	$top[] = "<a href='#' onClick='ajaxload(\"#content\",\"ajax/prof.php?p=" . $recipe['profID'] . "\")' class='small'><< " . $profName[$recipe['profID']] . "</a>";
}
echo  implode(" | ", $top);
unset($top);
echo "<div id='btnrecipes'>


<hr /><div id='recipes'>";
echo '<table class="tablesorter">' . "\n";
echo '<thead>';
echo '<tr><th>Known</th><th>Item</th><th>Qty</th><th>Cooldown</th><th>Moving Price</th><th>Crafting Price</th><th>Return</th><th>Yield</th></tr>';
echo '</thead><tbody>';
printRecipe($t->toArray());
unset($t);
echo '</tbody></table></div><br />';

function printRecipe(&$tree, $prefix = '') {
	$db = $GLOBALS['db'];
	$item = $db->items[$tree['id']];
	$known = '<img src="images/check.png" />';
	$unknown  = '<img src="images/delete.png" />';
	
	
		echo '<tr>' . "\n";
		echo '<td>';
		if (array_key_exists('known',$tree)) {
			if ($tree['known']) { echo $known; } else { echo $unknown; }
		}
		echo '</td>';
		echo '<td>' . $prefix;
		echo '<img src="images/icons/' . $item['icon'] . '" style="width:20px; height: 20px;" /> ' . $item['name'] . '</td>';
		if ($tree['in'] == 0) { $count = $tree['out']; } else { $count = round($tree['in'],2); }
		echo '<td>' . $count . '</td>' . "\n";
		if ($tree['in']) { $t = $tree['in']; }  else { $t = 1; }
		echo '<td>' . $tree['cd'] . 'h</td>';
		$vf = ($tree['price'] * $count);
		echo '<td>' . getGold($vf) . '</td>' . "\n";
		if (array_key_exists('cost', $tree)) {
			$vi = $tree['cost'] * $count;
			$cost = ""; $return = ""; $yield = "";
			if ($vi) { 
				$cost = getGold($vi); $return = getGold(round($vf - $vi)); $yield = round(safediv($vf - $vi,$vi)*100) . "%";
			}
			echo '<td>' . $cost . '</td>';
			echo "<td>$return</td>";
			echo '<td>' . $yield . '</td>';
		}
		echo '</tr>' . "\n";
		//Print inputs IF the tree contains any
		if (array_key_exists('inputs', $tree) && count($tree['inputs'] > 0)) {
			if (strpos($prefix, 'tree_new')) {
				$prefix = '<span style="margin-right:15px;">&nbsp;</span>' . $prefix;
			} else {
				$prefix = "<img style='position: relative; top: -5px;'src='images/tree_new.gif' /> ";
			}
			printInputs($tree, $prefix);
		}
	
}

function printInputs(&$tree, $prefix) {

	$db = $GLOBALS['db'];
	for ($i = 0; $i < count($tree['inputs']); $i++) {
		foreach($tree['inputs'][$i] as &$row) {
			if ( array_key_exists('inputs', $row) ) {
				printRecipe($row, $prefix);
			} else { 
				$item = $db->items[$row['id']];
				echo '<tr>' . "\n";
				echo '<td></td>';
				echo '<td>' . $prefix;
				echo '<img src="images/icons/' . $item['icon'] . '" style="width:20px; height: 20px;" /> ' . $item['name'] . '(' . $row['id'] . ')</td>';
				echo '<td>' . round($row['in'],1) . '</td>' . "\n";
				echo '<td>-</td>' . "\n";
				echo '<td>' . getGold($row['price'] * $row['in']) . '</td>';
				echo '<td></td><td></td><td></td>';
				echo '</tr>' . "\n";
			}
		}
	}
}

?>